package com.zg.invoice.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.zg.invoice.vo.OpenInvoiceReq;
import com.zg.invoice.vo.nuonuo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

/**
 * 诺诺网工具类
 */
@Component
@Slf4j
@RefreshScope
public class NuoNuoApiUtil {

    @Value("${invoice.nuoshuitong.requestUrl}")
    private  String url ;

    /**
     * 开票请求
     * @param openInvoiceReq
     * @return
     */
    public OpenInvoiceRes sendOpenInvoice(OpenInvoiceReq openInvoiceReq){
        String resultStr = HttpUtil
                .createPost(url +"/salescore/lan/invoice.do")
                .contentType("application/json;charset=UTF-8")
                .header("e-app-token", "eyJhbGciOiJIUzUxMiIsInppcCI6IkRFRiJ9.eNocjFEKwjAQBe-yn9JAdrPZbHqYQmKjVKGVphVFvLupvzNv3gdu2wQ9-MxK3o_GeZsM45hNynIxanNIJWhEd4YO6p7bGNFbJRUhttFFQtLhNGDzU63Nb8u9zKaW9VnWA6YNegxoGUnQdVBeD-i59VaFDpDm97hc6_-ahSN6Ds4HtcRR4PsDAAD__w.yGY0pzMrbOstN2EgubaIryZO0gErtnRVKakwqmHxvpz5isAOx3udM4aw_4Z7qfBuLx_fP8-DgSjspkeNs19a0Q")
                .body(JSON.toJSONString(openInvoiceReq))
                .execute().body();
        log.info("请求地址："+url+";请求参数："+ JSON.toJSONString(openInvoiceReq));
        log.info("响应结果："+resultStr);
        OpenInvoiceRes openInvoiceRes = JSON.toJavaObject(JSON.parseObject(resultStr), OpenInvoiceRes.class);
        return openInvoiceRes;
    }

    /**
     * 发票作废
     */
    public InvalidInvoiceRes sendInvalidInvoice(InvalidInvoiceReq invalidInvoiceReq){

        String result = HttpUtil
                .createPost(url+"/salescore/lan/invalid-oneInvoice.do")
                .contentType("application/json;charset=UTF-8")
                .body(JSONObject.toJSONString(invalidInvoiceReq))
                .execute()
                .body();
        log.info("请求url:"+url+";发票作废请求参数:"+ JSONObject.toJSONString(invalidInvoiceReq));
        log.info("发票作废返回结果:"+result);
        InvalidInvoiceRes invalidInvoiceRes = JSON.toJavaObject(JSON.parseObject(result), InvalidInvoiceRes.class);
        return invalidInvoiceRes;
    }

    /**
     *发票打印
     */
    public InvoicePrintRes sendInvoicePrint(InvoicePrintReq invoicePrintReq){

        String result = HttpUtil
                .createPost(url+"/salescore/lan/print/batchPrintInvoiceDecrypt.do")
                .contentType("application/json;charset=UTF-8")
                .body(JSONObject.toJSONString(invoicePrintReq))
                .execute()
                .body();
        log.info("请求url:"+url+";发票打印请求参数:"+ JSONObject.toJSONString(invoicePrintReq));
        log.info("发票打印返回结果"+result);
        InvoicePrintRes invoicePrintRes = JSON.toJavaObject(JSONObject.parseObject(result), InvoicePrintRes.class);
        log.info(invoicePrintRes.toString());
        String printString = "webprint:"+invoicePrintRes.getData();
        log.info("返回给前端调用诺诺云打印的字符串:"+printString);
        return invoicePrintRes;
    }
    /**
     *发票红冲
     */
    public InvoiceRedRes sendInvoiceRed(InvoiceRedReq invoiceRedReq){
        String result = HttpUtil
                .createPost(url+"/salescore/lan/fastRepeatedRedSingle.do")
                .contentType("application/json;charset=utf-8")
                .body(JSONObject.toJSONString(invoiceRedReq))
                .execute()
                .body();
        log.info("发票红冲请求报文:"+invoiceRedReq.toString());
        log.info("发票红冲返回报文"+result);
        InvoiceRedRes invoiceRedRes = JSON.toJavaObject(JSON.parseObject(result), InvoiceRedRes.class);
        log.info("转成对象："+invoiceRedRes.toString());
        return invoiceRedRes;
    }
    /**
     *开票结果查询-定时
     */
    public OpenInvoiceResultRes sendOpenInvoiceResult(OpenInvoiceResultReq openInvoiceResultReq){
        String resultStr = HttpUtil
                .createPost(url+"/salescore/lan/query-invoice.do")
                .contentType("application/json;charset=UTF-8")
                .body(JSON.toJSONString(openInvoiceResultReq))
                .execute().body();
        log.info("请求参数："+ JSON.toJSONString(openInvoiceResultReq));
        log.info("响应结果："+resultStr);
        OpenInvoiceResultRes openInvoiceResultRes = JSON.toJavaObject(JSONObject.parseObject(resultStr), OpenInvoiceResultRes.class);
        return openInvoiceResultRes;
    }
    /**
     *抄报清卡
     */
    public InvoiceReportAndClearRes sendInvoiceReportAndClear(InvoiceReportAndClearReq invoiceReportAndClearReq){
        String result = HttpUtil
                .createPost(url+"/salescore/extensioninfo/reportAndClear.do")
                .contentType("application/json;charset=utf-8")
                .body(JSONObject.toJSONString(invoiceReportAndClearReq))
                .execute()
                .body();

        log.info("抄报清卡请求报文:"+invoiceReportAndClearReq.toString());
        log.info("抄报清卡返回报文:"+result);
        InvoiceReportAndClearRes invoiceReportAndClearRes = JSON.toJavaObject(JSON.parseObject(result), InvoiceReportAndClearRes.class);
        log.info("转成返回对象:"+invoiceReportAndClearRes);
        return invoiceReportAndClearRes;
    }

    /**
     *冲红申请接口
     */
    public InvoiceRedRes sendInvoiceRedApply(InvoiceRedApplyReq invoiceRedApplyReq){
        String result = HttpUtil
                .createPost(url+"/salescore/lan/redConfirm/apply.do")
                .contentType("application/json;charset=utf-8")
                .body(JSONObject.toJSONString(invoiceRedApplyReq))
                .execute()
                .body();
        log.info("发票冲红申请请求报文:{}", JSONUtil.toJsonStr(invoiceRedApplyReq));
        log.info("发票冲红申请返回报文:{}",result);
        InvoiceRedRes invoiceRedRes = JSON.toJavaObject(JSON.parseObject(result), InvoiceRedRes.class);
        log.info("冲红申请转成对象：{}", JSONUtil.toJsonStr(invoiceRedRes));
        return invoiceRedRes;
    }

    /**
     *查询蓝票全电号码
     */
    public InvoiceRedRes queryInvoice(QueryInvoiceReq queryInvoiceReq){
        String result = HttpUtil
                .createPost(url+"/salescore/lan/query-invoice.do")
                .contentType("application/json;charset=utf-8")
                .body(JSONObject.toJSONString(queryInvoiceReq))
                .execute()
                .body();
        log.info("发票信息查询请求报文:{}", JSONUtil.toJsonStr(queryInvoiceReq));
        log.info("发票信息查询返回报文:{}",result);
        InvoiceRedRes invoiceRedRes = JSON.toJavaObject(JSON.parseObject(result), InvoiceRedRes.class);
        log.info("发票信息查询转成对象：{}", JSONUtil.toJsonStr(invoiceRedRes));
        return invoiceRedRes;
    }

    /**
     *查询红冲确认单状态
     */
    public InvoiceRedRes queryRedInvoiceInfo(InvoiceRedSingleReq invoiceRedSingleReq){
        String result = HttpUtil
                .createPost(url+"/salescore/lan/redConfirm/query.do")
                .contentType("application/json;charset=utf-8")
                .body(JSONObject.toJSONString(invoiceRedSingleReq))
                .execute()
                .body();
        log.info("查询红冲确认单状态报文:"+invoiceRedSingleReq.toString());
        log.info("查询红冲确认单状态返回报文"+result);
        InvoiceRedRes invoiceRedRes = JSON.toJavaObject(JSON.parseObject(result), InvoiceRedRes.class);
        log.info("转成对象："+invoiceRedRes.toString());
        return invoiceRedRes;
    }
}
